package com.pojoin.cms.action;

import java.util.List;

import org.springframework.beans.BeanUtils;

import com.pojoin.cms.domain.Resource;
import com.pojoin.cms.domain.form.ResourceForm;
import com.pojoin.cms.exception.CmsException;
import com.pojoin.cms.face.ResourceFace;
import com.pojoin.cms.face.ResourceFaceFactory;
import com.pojoin.cms.service.ResourceService;

@SuppressWarnings("serial")
public class ResourceAction extends PagerAction {

	private ResourceService resourceService;
	private ResourceFaceFactory resourceFaceFactory;
	private ResourceForm resource;
	private List<Resource> list;
	public ResourceService getResourceService() {
		return resourceService;
	}
	public void setResourceService(ResourceService resourceService) {
		this.resourceService = resourceService;
	}
	public ResourceFaceFactory getResourceFaceFactory() {
		return resourceFaceFactory;
	}
	public void setResourceFaceFactory(ResourceFaceFactory resourceFaceFactory) {
		this.resourceFaceFactory = resourceFaceFactory;
	}
	public ResourceForm getResource() {
		return resource;
	}
	public void setResource(ResourceForm resource) {
		this.resource = resource;
	}
	public List<Resource> getList() {
		return list;
	}
	public void setList(List<Resource> list) {
		this.list = list;
	}
	
	
	/**
	 * 请求添加
	 * @return
	 */
	public String willAdd(){
		this.list = this.resourceService.getAllResources();
		return this.success("resource/add.jsp");
	}
	
	public String add(){
		Resource resource = this.resourceService.add(this.resource);
		if(resource == null){
			this.msg = "资源已经存在";
		}
		return this.list();
	}
	
	/**
	 * 删除
	 * @return
	 * @throws CmsException 
	 */
	public String del() throws CmsException{
		ResourceFace resourceFace = this.resourceFaceFactory.getResourceFaceById(this.resource.getResourceId());
		resourceFace.delete();
		return this.list();
	}
	
	/**
	 * 加载数据
	 * @return
	 */
	public String load(){
		Resource r = this.resourceService.findById(this.resource.getResourceId());
		BeanUtils.copyProperties(r, this.resource);
		this.list = this.resourceService.getAllResources();
		return this.success("resource/update.jsp");
	}
	
	/**
	 * 更新
	 * @return
	 * @throws CmsException 
	 */
	public String update() throws CmsException{
		ResourceFace rf = this.resourceFaceFactory.getResourceFaceById(this.resource.getResourceId());
		rf.update(this.resource);
		rf.flush();
		return this.list();
	}
	
	/**
	 * 列表页
	 * @return
	 */
	public String list(){
		this.list = this.resourceService.getAllResources();
		return this.success("resource/list.jsp");
	}
	
	
}
